﻿
app.controller('LawController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    
    $scope.GetLawCategoryList = function () {
        $scope.CategoryList = [];
        $http({
            method: 'POST',
            url: '../Law/GetLawCategoryList'
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.CategoryList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }
    
    $scope.GetOrgLevel = function () {
        $scope.OrgLevelList = [];
        $http({
            method: 'POST',
            url: '../Law/GetOrgLevel'
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.OrgLevelList = ret.Data;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.BindOrgList = function (control, defaultvalue) {
        $("#" + control).empty();
        $http({
            method: 'POST',
            url: '../Law/GetOrgList',
        })
            .success(function (ret) {
                $scope.HarmList = ret.Data;
                $.each(ret.Data, function (i, n) {
                    if (defaultvalue != null && defaultvalue.length > 0) {
                        if (defaultvalue.includes(n.Id))
                            $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.OrgName + "</option>")($scope));
                        else
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.OrgName + "</option>")($scope));
                    }
                    else {
                        $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.OrgName + "</option>")($scope));
                    }

                })
                $("#" + control).selectpicker('refresh');
            })
            .error(function (data, status, headers, config) {
            });
    }

    $scope.SubmitDocument = function (model, status,page) {
        if (model.DocumentNo == null || model.DocumentNo=="") {
            alertify.alert("请输入文件编号！");
            return false;
        }
        if (model.DocumentName == null || model.DocumentName == "") {
            alertify.alert("请输入文件名称！");
            return false;
        }
        $scope.DataLoading = true;
        var Orglist = $("#select_org").val();
        var files = document.querySelector('input[type=file]').files;
        model.Status = status;
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../Law/AddLawForm',
            data: { 'model': model, "OrgList": Orglist},
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                if (model.status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("发布成功！");
                }
                setTimeout(function () {
                    window.location.href = "LawDocumentPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.EditDocument = function (model, status,page) {
        if (model.DocumentNo == null || model.DocumentNo == "") {
            alertify.alert("请输入文件编号！");
            return false;
        }
        if (model.DocumentName == null || model.DocumentName == "") {
            alertify.alert("请输入文件名称！");
            return false;
        }
        $scope.DataLoading = true;
        var Orglist = $("#select_org").val();
        var files = document.querySelector('input[type=file]').files;
        model.Status = status;
        $scope.DataLoading = true;
        Upload.upload({
            method: 'POST',
            url: '../Law/EditLawForm',
            data: { 'model': model, "OrgList": Orglist },
            file: files
        }).success(function (ret) {
            $scope.DataLoading = false;
            if (ret.Code == 100) {
                if (model.Status == 0) {
                    alertify.alert("保存成功！");
                }
                else {
                    alertify.alert("发布成功！");
                }
                setTimeout(function () {
                    window.location.href = "LawDocumentPageList?page=" + page;
                }, 3000);
            } else
                alertify.alert(ret.Msg);
        })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })

    }

    $scope.GetLawFormPageList = function (model, page) {
        $scope.DataLoading = true;
        var orglist = $("#select_org").val();
        model.PublishOrg = orglist;
        $scope.LawFormList = [];
        $http({
            method: 'POST',
            url: '../Law/GetLawFormPageList',
            data: { 'model': model,'page':page}
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.LawFormList = ret.Data.Data;
                $scope.TotalCount = ret.Data.TotalItemCount;
                console.log($scope.result);
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }
    $scope.GetLawFormBookList = function (model, page) {
        $scope.DataLoading = true;
        $scope.LawFormList = [];
        var orglist = $("#select_org").val();
        model.PublishOrg = orglist;
        $http({
            method: 'POST',
            url: '../Law/GetLawFormBookList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.LawFormList = ret.Data.Data;
                $scope.TotalCount = ret.Data.TotalItemCount;
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }
    $scope.AddPublishOrg = function (model) {
        var orglist = [];
        var selecedorg = $("#select_org").val();
        if (selecedorg!=null && selecedorg.length > 0) {
            for (let i = 0; i < selecedorg.length; i++) {
                orglist.push(parseInt(selecedorg[i]));
            }
        }
        $http({
            method: 'POST',
            url: '../Law/AddPublishOrg',
            data: { 'OrgName': model.OrgName}
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("保存成功！");
                    common.success($scope, null, ret);
                    $scope.BindOrgList('select_org', orglist);
                    model.OrgName = "";
                    $("#btn_close").click();
                }
              
                
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.GetLawFormModel = function (id) {
        $scope.LawForm = {};
        $http({
            method: 'POST',
            url: '../Law/GetLawDocumentModel',
            data: { 'Id': id }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                common.success($scope, null, ret);
                $scope.LawForm = ret.Data;
                $scope.BindOrgList('select_org', $scope.LawForm.PublishOrgId);
                $scope.LawForm.CreateTime = $filter('JSONDate')($scope.LawForm.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                $scope.LawForm.CategoryId = $scope.LawForm.CategoryId + "";
                $scope.LawForm.OrgLevelId = $scope.LawForm.OrgLevelId + "";
                $scope.LawForm.ValidTime = $filter('JSONDate')($scope.LawForm.ValidTime, 'yyyy-MM-dd');
                $scope.LawForm.PublishTime = $filter('JSONDate')($scope.LawForm.PublishTime, 'yyyy-MM-dd');
               
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
            });
    }

    $scope.DeleteLawForm = function (model, page, searchmodel) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除【" + model.DocumentName+"】？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Law/DeleteLawForm',
                    data: { 'FormId': model.Id}
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        if (ret.Code == 100) {
                            alertify.alert("操作成功！");
                            $scope.GetLawFormPageList(searchmodel, page);
                        } else
                            alertify.alert(ret.Msg);

                    })
                    .error(function (data, status, headers, config) {
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }
    $scope.UpdateLawStatus = function (model, status, page, searchmodel) {
        var text = "发布";
        if (status == 0) {
            text="撤回"
        }
        else if (status == 2) {
            text = "作废"
        }
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认" + text+"【" + model.DocumentName + "】？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../Law/UpdateLawStatus',
                    data: {'Status':status, 'FormId': model.Id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        if (ret.Code == 100) {
                            alertify.alert("操作成功！");
                            $scope.GetLawFormPageList(searchmodel, page);
                        } else
                            alertify.alert(ret.Msg);

                    })
                    .error(function (data, status, headers, config) {
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }
});




app.filter('HideCardNo', function () { //可以注入依赖
    return function (text) {
        if (text != null && text != "")
            return text.replace(/^(.{3})(?:\d+)(.{4})$/, "$1******$2");
    }
});
